package com.xuecheng.test.freemarker.controller;

import com.xuecheng.test.freemarker.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller   //切忌不能用RestController
@RequestMapping("/freemarker")
public class  FreemarkerController  {

    /**
     * 初次测试freemarker   这里的return test1，将直接指定模板名称为test1的ftl文件
     * @param map
     * @return
     */
    @RequestMapping("/test1")
    public String freemarkerTest1(Map<String,Object> map){
        map.put("name","测试数据");
        return "test1";
    }

    /**
     * 测试list，map的遍历，if指令使用
     * @param map
     * @return
     */
    @RequestMapping("/test2")
    public String freemarkerTest2(Map<String,Object> map){
        Student stu1 = new Student();
        stu1.setName("小明");
        stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge(19);
        stu2.setBirthday(new Date());

        //准备list数据
        List<Student> friends = new ArrayList<>();
        friends.add(stu1);
        friends.add(stu2);
        //向数据模型放数据
        map.put("studentList",friends);

        //准备map数据
        HashMap<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        //向数据模型放数据
        map.put("studentMap",stuMap);

        //返回模板文件名称
        return "test2";
    }

    /**
     * 测试空字符替换处理
     * @param map
     * @return
     */
    @RequestMapping("/test3")
    public String freemarkerTest3(Map<String,Object> map){
        Student stu1 = new Student();
        stu1.setName("小明");
        //stu1.setAge(18);
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());
        Student stu2 = new Student();
        stu2.setName("小红");
        //stu2.setMoney(200.1f);
        stu2.setAge(19);
        stu2.setBirthday(new Date());

        //准备list数据
        List<Student> friends = new ArrayList<>();
        friends.add(stu1);
        friends.add(stu2);
        //向数据模型放数据
        map.put("studentList",friends);

        //准备map数据
        HashMap<String,Student> stuMap = new HashMap<>();
        stuMap.put("stu1",stu1);
        stuMap.put("stu2",stu2);
        //向数据模型放数据
        map.put("studentMap",stuMap);

        //返回模板文件名称
        return "test3";
    }

}
